package com.sr.controller;

import com.sr.annotation.Log;
import com.sr.constant.Constant;
import com.sr.enums.ResponseCode;
import com.sr.pojo.SysRole;
import com.sr.service.IRoleService;
import com.sr.util.R;
import com.sr.vo.PageVo;
import com.sr.vo.req.RoleReqVo;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * @author xiaochi
 * @date 2022/5/3 18:21
 * @desc RoleController
 */
@Api(tags = "后台接口 -> 角色模块")
@RestController
@RequestMapping(Constant.Api.BACK_PREFIX)
@RequiredArgsConstructor(onConstructor = @__)
public class RoleController extends BaseController {

    private final IRoleService roleService;

    @ApiOperation(value = "分页获取角色数据接口",notes = "分页获取角色数据接口")
    @PreAuthorize("hasAuthority('sys:role:list')")
    @Log(module = "组织管理-角色管理",action = "分页获取角色数据接口")
    @PostMapping("/roles")
    public R<PageVo<SysRole>> pageInfo(@RequestBody RoleReqVo reqVo){
        SysRole sysRole = SysRole.builder().deleted(1).build();
        return R.ok(roleService.findPage(reqVo.getPageNum(),reqVo.getPageSize(),sysRole));
    }

    @ApiOperation(value = "新增角色接口",notes = "新增角色")
    @PreAuthorize("hasAuthority('sys:role:add')")
    @Log(module = "组织管理-角色管理",action = "新增角色接口")
    @PostMapping("/role")
    public R<String> createRole(@RequestBody @Validated(RoleReqVo.Create.class) RoleReqVo roleReqVo){
        return this.response(roleService.createRole(roleReqVo));
    }

    @ApiOperation(value = "获取角色详情接口",notes = "获取角色详情接口")
    @PreAuthorize("hasAuthority('sys:role:detail')")
    @Log(module = "组织管理-角色管理",action = "获取角色详情接口")
    @GetMapping("/role/{id}")
    public R<Set<String>> getRoleBy(@PathVariable("id") String roleId){
        return roleService.getRoleById(roleId);
    }

    @ApiOperation(value = "更新角色信息",notes = "更新角色信息接口")
    @PreAuthorize("hasAuthority('sys:role:update')")
    @Log(module = "组织管理-角色管理",action = "更新角色信息接口")
    @PutMapping("/role")
    public R<String> updateRole(@RequestBody @Validated(RoleReqVo.Update.class) RoleReqVo reqVo){
        return this.response(roleService.updateRole(reqVo));
    }

    @ApiOperation(value = "更新角色状态",notes = "更新角色状态接口")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "String",paramType = "form"),
        @ApiImplicitParam(name = "status",value = "状态status",required = true,dataType = "Integer",paramType = "form"),
    })
    @PreAuthorize("hasAuthority('sys:role:update')")
    @Log(module = "组织管理-角色管理",action = "更新角色状态接口")
    @PostMapping("/role/{id}/{status}")
    public R<String> updateRoleStatus(@PathVariable("id")String roleId,@PathVariable("status") Integer status){
        if (StringUtils.isBlank(roleId)|| ObjectUtils.isEmpty(status)){
            return R.error(ResponseCode.DATA_ERROR.getMessage());
        }
        SysRole sysRole = new SysRole();
        sysRole.setId(roleId);
        sysRole.setStatus(status);
        return this.response(roleService.updateById(sysRole));
    }

    @ApiOperation(value = "删除角色信息",notes = "删除角色接口")
    @PreAuthorize("hasAuthority('sys:role:delete')")
    @Log(module = "组织管理-角色管理",action = "删除角色接口")
    @DeleteMapping("/role/{id}")
    public R<String> deletedRole(@PathVariable("id") String roleId){
        return this.response(roleService.deletedRole(roleId));
    }
}
